package com.gobuy.goods.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gobuy.goods.dao.BrandMapper;
import com.gobuy.goods.pojo.Brand;
import com.gobuy.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * @author Gaozexi on 2020/6/2 21:45
 * Step by Step  and Stand on,  You Are The Best Investment!
 */
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;

    /**
     * 品牌列表查询
     * @return
     */
    @Override
    public List<Brand> findList() {
        return brandMapper.selectAll();
    }

    @Override
    public Brand findById(Integer id) {
        return brandMapper.selectByPrimaryKey(id);
    }


    /**
     * DML方法需要被事务管理
     */
    @Override
    @Transactional
    public void add(Brand brand) {
        brandMapper.insertSelective(brand);
    }

    @Override
    @Transactional
    public void update(Brand brand) {
        brandMapper.updateByPrimaryKey(brand);
    }

    @Override
    public void delById(Integer id) {
        brandMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Brand> searchByCondition(Map<String, Object> searchMap) {
        Example example = new Example(Brand.class);
        //封装查询条件
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            //品牌名字模糊查询
            if(searchMap.get("name")!=null&&!"".equals(searchMap.get("name"))){
                //andLike(String property, String value) 对哪个字段(name) 进行何种方式(andLIke)查询 ，匹配什么值searchMap.get("name")
                criteria.andLike("name","%"+searchMap.get("name")+"%");
            }
            //对首字母进行等值查询
            if(searchMap.get("letter")!=null&&!"".equals(searchMap.get("letter"))){
                criteria.andEqualTo("letter",searchMap.get("letter"));
            }
        }
        return brandMapper.selectByExample(example);
    }

    @Override
    public Page<Brand> findPage(int page, int size) {
        //给pageHelper传入参数，由它来完成结果集的分页，实现原理是拦截器
        PageHelper.startPage(page,size);
        Page<Brand> pageInfo = (Page<Brand>) brandMapper.selectAll();
        return pageInfo;
    }

    @Override
    public Page<Brand> findPage(Map<String, Object> searchMap, int page, int size) {
        //给pageHelper传入参数，由它来完成结果集的分页，实现原理是拦截器
        PageHelper.startPage(page,size);
        Example example = new Example(Brand.class);
        //封装查询条件
        Example.Criteria criteria = example.createCriteria();
        if(searchMap!=null){
            //品牌名字模糊查询
            if(searchMap.get("name")!=null&&!"".equals(searchMap.get("name"))){
                //andLike(String property, String value) 对哪个字段(name) 进行何种方式(andLIke)查询 ，匹配什么值searchMap.get("name")
                criteria.andLike("name","%"+searchMap.get("name")+"%");
            }
            //对首字母进行等值查询
            if(searchMap.get("letter")!=null&&!"".equals(searchMap.get("letter"))){
                criteria.andEqualTo("letter",searchMap.get("letter"));
            }
        }
        Page<Brand> pageInfo = (Page<Brand>) brandMapper.selectByExample(example);
        return pageInfo;
    }

    @Override
    public List<Map> findBrandListByCategoryName(String categoryName) {
        return brandMapper.findBrandListByCategoryName(categoryName);
    }
}
